package com.template.ejb.service.bean;

import java.util.List;

import javax.ejb.EJB;
import javax.ejb.Stateless;

import com.template.ejb.datasource.PersonDAO;
import com.template.ejb.domain.Person;
import com.template.ejb.service.UserService;

@Stateless
public class UserServiceBean implements UserService {

	@EJB
	private PersonDAO personDAO;

	public void setPersonDAO(PersonDAO personDAO) {
		this.personDAO = personDAO;
	}

	@Override
	public void create(Person person) {
		if (personDAO.findByLogin(person.getLogin()) != null) {
			throw new IllegalArgumentException("Person with login "
					+ person.getLogin() + " already exists!");
		} else {
			personDAO.create(person);
		}
	}

	@Override
	public Person update(Person person) {
		return personDAO.update(person);
	}

	@Override
	public Person findById(Long id) {
		return personDAO.findById(id);
	}

	@Override
	public Person findByLogin(String login) {
		return personDAO.findByLogin(login);
	}

	@Override
	public List<Person> getPersons(int offset, int limit) {
		return personDAO.getPersons(offset, limit);
	}
}
